//
//  JCReportWeeklyDetailVC.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/11/30.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCReportWeeklyDetailVC.h"
#import "JCReportStatisticsVC.h"
#import "JCReportProposalVC.h"
#import "JCReportCommentsVC.h"
#import "JCReportModel.h"
#import "JCClubApi.h"
#import "UIImage+YFExtension.h"
#import "UIView+JCDrawTool.h"
#import "MyNavigationViewController.h"

@interface JCReportWeeklyDetailVC ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIView *lineView;
@property (weak, nonatomic) IBOutlet UIView *cutLine;
@property (weak, nonatomic) IBOutlet UIButton *proposalButton;
@property (weak, nonatomic) IBOutlet UIButton *statisticsButton;
@property (weak, nonatomic) IBOutlet UIButton *commentsButton;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *toolButtons;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong, nonatomic) JCReportStatisticsVC *statisticVC;
@property (strong, nonatomic) JCReportProposalVC *proposalVC;
@property (strong, nonatomic) JCReportCommentsVC *commentsVC;

@end

@implementation JCReportWeeklyDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCode];
}

- (void)initCode{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         [_scrollView setContentOffset:CGPointMake(kYFScreenWidth, 0) animated:NO];
    });
    _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 3)];
    _lineView.backgroundColor = [UIColor yellowColor];
    _lineView.center = CGPointMake(kYFScreenWidth / 2.0, 0.25);
    [self.cutLine addSubview:_lineView];
    
    self.navigationItem.title = @"Sports Weekly";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share_icon"] style:UIBarButtonItemStyleDone target:self action:@selector(shareAction)];
    ((MyNavigationViewController *)self.navigationController).hiddenSeparationLine = YES;
}

- (void)shareAction{
    [self.hud showWithMaskType:WSProgressHUDMaskTypeClear];
    NSInteger select = 0;
    for (UIButton *btn in _toolButtons) {
        if (btn.selected) {
            select = btn.tag;
        }
    }
    
    NSArray <UIImage *> *imageArray;
    CGFloat height = 0;
    if (_commentsVC.commentModel) {
        UIImage *imageLeft = [UIImage imageFromView:_proposalVC.scrollview];
        UIImage *imageCenter = [UIImage imageFromView:_statisticVC.tableView];
        UIImage *imageRight = [UIImage imageFromView:_commentsVC.tableView];
        height = imageLeft.size.height + imageCenter.size.height + imageRight.size.height;
        imageArray = @[imageLeft,imageCenter,imageRight];
    }else{
        UIImage *imageLeft = [UIImage imageFromView:_proposalVC.scrollview];
        UIImage *imageCenter = [UIImage imageFromView:_statisticVC.tableView];
        height = imageLeft.size.height + imageCenter.size.height;
        imageArray = @[imageLeft,imageCenter];
    }
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIGraphicsBeginImageContext(CGSizeMake(kYFScreenWidth, height));
        CGFloat pindHeigh = 0;
        for (NSInteger i = 0; i < imageArray.count; i++) {
            UIImage *image = imageArray[i];
            if (i > 0) {
                pindHeigh += imageArray[i - 1].size.height;
            }
            [image drawInRect:CGRectMake(0, pindHeigh, kYFScreenWidth, image.size.height)];
        }
        UIImage *drawImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.hud dismiss];
            UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[drawImage] applicationActivities:nil];
            [self.navigationController presentViewController:activityController animated:YES completion:nil];
        });
    });
}

- (IBAction)toolButtonAction:(UIButton *)sender {
    [self.view endEditing:YES];
    for (UIButton *btn in _toolButtons) {
        btn.selected = NO;
    }
    sender.selected = YES;
    [_scrollView setContentOffset:CGPointMake(kYFScreenWidth * sender.tag, 0) animated:YES];
    [UIView animateKeyframesWithDuration:.3 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{
       _lineView.center = CGPointMake(kYFScreenWidth / 6.0 * (1+2*sender.tag), 0.25);
    } completion:^(BOOL finished) {
        
    }];
    
    if (sender.tag == 2 && _commentsVC.commentModel == nil) {
        self.navigationItem.rightBarButtonItem = nil;
    }else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share_icon"] style:UIBarButtonItemStyleDone target:self action:@selector(shareAction)];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
//        CGFloat offset = scrollView.contentOffset.x;
//        _lineView.center = CGPointMake(kYFScreenWidth / 2.0, _cutLine.bounds.origin.y);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"proposalEmbed"]) {
        _proposalVC = segue.destinationViewController;
        _proposalVC.reportListModel = _reportListModel;
        _proposalVC.userID = _userID;
        _proposalVC.clubID = _clubID;
    }else if ([segue.identifier isEqualToString:@"statisticsEmbed"]) {
        _statisticVC = segue.destinationViewController;
        _statisticVC.userID = _userID;
        _statisticVC.clubID = _clubID;
        _statisticVC.reportListModel = _reportListModel;
    }else if ([segue.identifier isEqualToString:@"commentEmbed"]){
        _commentsVC = segue.destinationViewController;
        _commentsVC.clubID = _clubID;
        _commentsVC.userType = [[User getUseInfo] clubUserType];
        _commentsVC.userID = _userID;
        _commentsVC.year = _reportListModel.year;
        _commentsVC.yearWeek = _reportListModel.yearWeek;
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
